<?php
	/**
	This file is part of PEL.
	/**
	 * Elgg pel plugin
	 */

		function pel_init(){
			// Load system configuration
			global $CONFIG;
				
			// Load the language file
			register_translations($CONFIG->pluginspath . "pel/languages/");
				
			// Set up menu for logged in users
			if (isloggedin()){
				add_menu(elgg_echo('pel'), $CONFIG->wwwroot . "pg/pel/" . $_SESSION['user']->username);
			}
      else{
				add_menu(elgg_echo('pel'), $CONFIG->wwwroot . "mod/pel/everyone.php",array());
			}
				
			// Extend system CSS with our own styles, which are defined in the pel/css view
			if(get_context()=='pel'){
				extend_view('metatags','js/peltabs');
			}
			
			extend_view('css','pel/css');
			
			
			extend_view('profile/menu/links','pel/menu');
			// Register a page handler, so we can have nice URLs
			register_page_handler('pel','pel_page_handler');
				
			// Register a URL handler for pel posts
			register_entity_url_handler('pel_url','object','lang');
				
			// Register this plugin's object for sending pingbacks
			register_plugin_hook('pingback:object:subtypes', 'object', 'pel_pingback_subtypes');
				
			// Register entity type
			register_entity_type('object','pel');
		}
		
		function pel_pagesetup(){
			global $CONFIG;
			//add submenu options
			if (get_context() == "pel"){
				if ((page_owner() == $_SESSION['guid'] || !page_owner()) && isloggedin()){
					add_submenu_item(elgg_echo('pel:your'),$CONFIG->wwwroot."pg/pel/" . $_SESSION['user']->username);
          $page_owner = page_owner_entity();
					 if(page_owner()){
						$page_owner = page_owner_entity();
						if(get_user_objects($page_owner->getGUID(),'pel',10)){
						add_submenu_item(elgg_echo('pel:addlang'),$CONFIG->wwwroot."mod/pel/addlang.php");
						add_submenu_item(elgg_echo('pel:edit'),$CONFIG->wwwroot."mod/pel/peledit.php");
						add_submenu_item(elgg_echo('pel:tablehelp'),$CONFIG->wwwroot."mod/pel/fichier/tableeval.pdf\" onclick=\"window.open(this.href,'','width=800,height=600,left=62,top=300,toolbar=no,status=no,location=no,menubar=no,directories=no');event.returnValue = false; return false; \""); 
						}
					}
					add_submenu_item(elgg_echo('pel:friends'),$CONFIG->wwwroot."pg/pel/" . $_SESSION['user']->username . "/friends/");
					add_submenu_item(elgg_echo('pel:everyone'),$CONFIG->wwwroot."mod/pel/everyone.php");
				}
        else if (page_owner()){
					$page_owner = page_owner_entity();
					if ($page_owner instanceof ElggUser) // Sorry groups, this isn't for you.
						add_submenu_item(elgg_echo('pel:mine'),$CONFIG->wwwroot."pg/pel/" . $_SESSION['user']->username);
					add_submenu_item(elgg_echo('pel:everyone'),$CONFIG->wwwroot."mod/pel/everyone.php");
				}
        else{
					add_submenu_item(elgg_echo('pel:everyone'),$CONFIG->wwwroot."mod/pel/everyone.php");
				}
			}
		}
		
		/**
		 * pel page handler; allows the use of fancy URLs
		 */
		function pel_page_handler($page){
			// The first component of a pel URL is the username
			if (isset($page[0])){
				set_input('username',$page[0]);
			}
			// The second part dictates what we're doing
			if (isset($page[1])){
				switch($page[1]){
					case "read":		set_input('lang',$page[2]);
						@include(dirname(__FILE__) . "/viewlang.php");
						return true;
						break;
					case "friends":		@include(dirname(__FILE__) . "/friends.php");
						return true;
						break;
				}
			// If the URL is just 'pel/username', or just 'pel/', load the standard pel index
			}
      else{
				@include(dirname(__FILE__) . "/index.php");
				return true;
			}
			return false;
		}

		/**
		 * Populates the ->getUrl() method for pel objects
		 */
	function pel_url($pellang){
			global $CONFIG;
			$title = $pellang->title;
			$title = friendly_title($title);
			return $CONFIG->url . "pg/pel/" . $pellang->getOwnerEntity()->username . "/read/" . $pellang->getGUID() . "/" . $title;
		}
		
		/**
		 * This function adds 'pel' to the list of objects which will be looked for pingback urls.
		 */
		function pel_pingback_subtypes($hook, $entity_type, $returnvalue, $params){
			$returnvalue[] = 'pel';
			return $returnvalue;
		}

	// Make sure the pel initialisation function is called on initialisation
		register_elgg_event_handler('init','system','pel_init');
		register_elgg_event_handler('pagesetup','system','pel_pagesetup');
		
	// Register actions
		global $CONFIG;
		register_action("pel/add",false,$CONFIG->pluginspath . "pel/actions/add.php");
		register_action("pel/deletelang",false,$CONFIG->pluginspath . "pel/actions/deletelang.php");
    register_action("pel/addlang",false,$CONFIG->pluginspath . "pel/actions/addlang.php");
    register_action("pel/editlang",false,$CONFIG->pluginspath . "pel/actions/editlang.php");
    register_action("pel/edit",false,$CONFIG->pluginspath . "pel/actions/edit.php");
?>